package com.qingpeng.pay.activity;

import android.content.Intent;
import android.view.View;

import com.qingpeng.pay.R;
import com.qingpeng.pay.api.Host;
import com.qingpeng.pay.api.RedBag;
import com.qingpeng.pay.base.BaseActivity;
import com.qingpeng.pay.beans.NullBean;
import com.qingpeng.pay.okhttp.RetrofitCreateHelper;
import com.qingpeng.pay.service.NLService;
import com.qingpeng.pay.service.NotificationCollectorMonitorService;
import com.qingpeng.pay.utils.AppManager;
import com.qingpeng.pay.utils.SpUtils;
import com.qingpeng.pay.utils.ToastUtils;
import com.qingpeng.pay.views.TitleBar;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SettingsActivity extends BaseActivity {

    @BindView(R.id.title)
    TitleBar title;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    protected int getRootViewId() {
        return 0;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void onResume() {
        super.onResume();
        title.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.miv_pwd, R.id.miv_pwd_pay, R.id.miv_gengxin, R.id.miv_out})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.miv_pwd:
                startActivity(new Intent(SettingsActivity.this, ForgetPwdActivity.class));
                break;
            case R.id.miv_pwd_pay:
                startActivity(new Intent(SettingsActivity.this, VerifyCodeActivity.class));
                break;
            case R.id.miv_gengxin:
                ToastUtils.showToast(SettingsActivity.this, "目前已是最新版本");
                break;
            case R.id.miv_out:
                Call<NullBean> start = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).start();
                start.enqueue(new Callback<NullBean>() {
                    @Override
                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                        NullBean body = response.body();
                        if (body.getStatus() != 1) {
                            ToastUtils.showToast(SettingsActivity.this, body.getInfo());
                            return;
                        }
                        stopService(new Intent(SettingsActivity.this, NotificationCollectorMonitorService.class));
                        stopService(new Intent(SettingsActivity.this, NLService.class));
                        ToastUtils.showToast(SettingsActivity.this, body.getInfo());
                    }

                    @Override
                    public void onFailure(Call<NullBean> call, Throwable t) {
                        ToastUtils.showToast(SettingsActivity.this, "网络请求错误,请稍后重试");
                    }
                });
                SpUtils.putBoolean(SettingsActivity.this, "islogin", false);
                SpUtils.putString(SettingsActivity.this, "uid", "");
                SpUtils.putString(SettingsActivity.this, "toke", "");
                SpUtils.putString(SettingsActivity.this, "userInfo", null);
                AppManager.getAppManager().finishAllActivity();
                startActivity(new Intent(SettingsActivity.this, LoginActivity.class));
                finish();
                break;
        }
    }
}
